package org.jupiter.blog.web.controller;

import org.jupiter.blog.api.dto.JupiterRolePermissionDTO;
import org.jupiter.blog.api.http.JupiterResult;
import org.jupiter.blog.service.service.JupiterRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role-permissions")
public class JupiterRolePermissionController {

	@Autowired
	private JupiterRolePermissionService rolePermissionService;

	@GetMapping("/{uuid}")
	public JupiterResult<JupiterRolePermissionDTO> getById(@PathVariable String uuid) {
		JupiterRolePermissionDTO rolePermission = rolePermissionService.getById(uuid);
		return JupiterResult.success(rolePermission);
	}

	@GetMapping
	public JupiterResult<List<JupiterRolePermissionDTO>> getAll() {
		List<JupiterRolePermissionDTO> rolePermissions = rolePermissionService.getAll();
		return JupiterResult.success(rolePermissions);
	}

	@GetMapping("/role/{roleId}")
	public JupiterResult<List<String>> getByRoleId(@PathVariable("roleId") String roleId) {
		List<String> rolePermissions = rolePermissionService.getByRoleId(roleId);
		return JupiterResult.success(rolePermissions);
	}

	@GetMapping("/permission/{permissionId}")
	public JupiterResult<List<JupiterRolePermissionDTO>> getByPermissionId(@PathVariable String permissionId) {
		List<JupiterRolePermissionDTO> rolePermissions = rolePermissionService.getByPermissionId(permissionId);
		return JupiterResult.success(rolePermissions);
	}

	@PostMapping
	public JupiterResult<Void> create(@RequestBody JupiterRolePermissionDTO rolePermission) {
		rolePermissionService.create(rolePermission);
		return JupiterResult.success();
	}

	@PutMapping("/{uuid}")
	public JupiterResult<Void> update(@PathVariable String uuid, @RequestBody JupiterRolePermissionDTO rolePermission) {
		rolePermission.setUuid(uuid);
		rolePermissionService.update(rolePermission);
		return JupiterResult.success();
	}

	@DeleteMapping("/{uuid}")
	public JupiterResult<Void> delete(@PathVariable String uuid) {
		rolePermissionService.delete(uuid);
		return JupiterResult.success();
	}

	@DeleteMapping("/role/{roleId}")
	public JupiterResult<Void> deleteByRoleId(@PathVariable("roleId") String roleId) {
		rolePermissionService.deleteByRoleId(roleId);
		return JupiterResult.success();
	}

	@DeleteMapping("/permission/{permissionId}")
	public JupiterResult<Void> deleteByPermissionId(@PathVariable String permissionId) {
		rolePermissionService.deleteByPermissionId(permissionId);
		return JupiterResult.success();
	}

	@PostMapping("/assign-permissions-to-role")
	public JupiterResult<Void> assignPermissionsToRole(@RequestBody JupiterRolePermissionDTO rolePermission) {
		rolePermissionService.assignPermissionsToRole(rolePermission);
		return JupiterResult.success();
	}

	@PostMapping("/assign-roles-to-permission/{permissionId}")
	public JupiterResult<Void> assignRolesToPermission(@PathVariable String permissionId,
			@RequestBody List<String> roleIds) {
		rolePermissionService.assignRolesToPermission(permissionId, roleIds);
		return JupiterResult.success();
	}
}
